package com.star.es.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.star.es.entity.ESQuery;
import com.star.es.entity.ESSearchBuilder;
import com.star.es.entity.QueryParam;
import com.star.es.service.FullTextQuery;

/**
 * 全文检索实现
 * @author Administrator
 */
@Service
public class FullTextQueryImpl implements FullTextQuery {
	
	private static final String FIELD_SPLIT=",";

	@Autowired
	private TransportClient client;

	@Override
	public SearchResponse matchQuery(QueryParam queryParam) {
		ESQuery esQuery = queryParam.convertESQuery();
		Map<String, Object> queryMap = queryParam.getQuery();
		queryMap.keySet().forEach(key -> esQuery.setQuery(QueryBuilders.matchQuery(key, queryMap.get(key))));
		return new ESSearchBuilder(client, esQuery).build();
	}

	@Override
	public SearchResponse multiMatchQuery(QueryParam queryParam) {
		ESQuery esQuery = queryParam.convertESQuery();
		Map<String, Object> queryMap = queryParam.getQuery();
		queryMap.keySet().forEach(key->{
			esQuery.setQuery(QueryBuilders.multiMatchQuery(queryMap.get(key), key.split(FIELD_SPLIT)));
		});
		return  new ESSearchBuilder(client, esQuery).build();
	}

	@Override
	public SearchResponse commonTermsQuery(QueryParam queryParam) {
		ESQuery esQuery = queryParam.convertESQuery();
		Map<String, Object> queryMap = queryParam.getQuery();
		queryMap.keySet().forEach(key->{
			esQuery.setQuery(QueryBuilders.commonTermsQuery(key, queryMap.get(key)));
		});
		return  new ESSearchBuilder(client, esQuery).build();
	}

	@Override
	public SearchResponse queryStringQuery(QueryParam queryParam) {
		ESQuery esQuery = queryParam.convertESQuery();
		Map<String, Object> queryMap = queryParam.getQuery();
		queryMap.keySet().forEach(key->{
			QueryStringQueryBuilder queryBuilder=new QueryStringQueryBuilder(queryMap.get(key).toString());
			Map<String, Float> fields=new HashMap<>();
			for(String field:key.split(FIELD_SPLIT)){
				fields.put(field, AbstractQueryBuilder.DEFAULT_BOOST);
			}
			queryBuilder.fields(fields);
			esQuery.setQuery(queryBuilder);
		});
		return  new ESSearchBuilder(client, esQuery).build();
	}

	@Override
	public SearchResponse simpleQueryString(QueryParam queryParam) {
		ESQuery esQuery = queryParam.convertESQuery();
		Map<String, Object> queryMap = queryParam.getQuery();
		queryMap.keySet().forEach(key->{
			QueryStringQueryBuilder queryBuilder=new QueryStringQueryBuilder(queryMap.get(key).toString());
			Map<String, Float> fields=new HashMap<>();
			for(String field:key.split(FIELD_SPLIT)){
				fields.put(field, AbstractQueryBuilder.DEFAULT_BOOST);
			}
			queryBuilder.fields(fields);
			esQuery.setQuery(queryBuilder);
		});
		return  new ESSearchBuilder(client, esQuery).build();
	}
	
}




































